package geektime.spring.cachedemo.service;


import static org.springframework.data.domain.ExampleMatcher.GenericPropertyMatchers.exact;

import geektime.spring.cachedemo.model.Coffee;
import geektime.spring.cachedemo.repository.CoffeeRepository;
import java.util.List;
import java.util.Optional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@CacheConfig(cacheNames = "coffee")
public class CoffeeService {

  @Autowired
  private CoffeeRepository coffeeRepository;

  @Cacheable
  public List<Coffee> findAllCoffee() {
    return coffeeRepository.findAll();
  }

  @CacheEvict
  public void reloadCoffee() {
  }

  public Optional<Coffee> findOneCoffee(String name) {
    ExampleMatcher matcher = ExampleMatcher.matching()
        .withMatcher("name", exact().ignoreCase());
    Optional<Coffee> coffee = coffeeRepository.findOne(
        Example.of(Coffee.builder().name(name).build(), matcher));
    log.info("Coffee Found: {}", coffee);
    return coffee;
  }
}
